package com.xiaoyu.izf.service;

import com.xiaoyu.izf.model.AppUpdateInfo;
import com.xiaoyu.izf.model.exception.BusinessException;
import com.xiaoyu.izf.model.exception.BusinessStatusEnum;
import com.xiaoyu.izf.repository.AppUpdateInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppUpdateInfoService extends BaseService{

    @Autowired
    private AppUpdateInfoDao appUpdateInfoDao;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /*public Map<String,Object> findLatest(){
        Map<String,Object> map = null;
        try {
            map = jdbcTemplate.queryForMap("select * from izf.app_update_info order by insert_time desc");
        }catch (Exception e){
            throw new BusinessException(BusinessStatusEnum.NOT_EXIST,"数据不存在");
        }
        return map;
    }*/

    public AppUpdateInfo findLatest(){
        List<AppUpdateInfo> list = appUpdateInfoDao.findAllByVersion();
        if(list!=null&&list.size()>0){
            return list.get(0);
        }else {
            throw new BusinessException(BusinessStatusEnum.NOT_EXIST,"数据不存在");
        }
    }

    public AppUpdateInfo findLastAndroidVersion(){
        String sql = "select * from app_update_info where type='android' order by app_version desc limit 0,1";
        AppUpdateInfo app = (AppUpdateInfo)jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper(AppUpdateInfo.class));
        return app;
    }

    public AppUpdateInfo findLastIOSVersion(){
        String sql = "select * from app_update_info where type='ios' order by app_version desc limit 0,1";
        AppUpdateInfo app = (AppUpdateInfo)jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper(AppUpdateInfo.class));
        return app;
    }
}
